﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using DTO;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http.Extensions;
using OtherLib.TenXun;
using FacadeApi.Extension;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MessageController : ControllerBase
    {
        public MessageApplication messageApplication { get; set; }
        public ILogger<MessageController> _logger { get; set; }

        [HttpGet]
        public ActionResult GetMessages()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = messageApplication.GetMessages(int.Parse(uid));
                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="from">{系统：0|管理员：1}</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult SetMessageRead( [FromForm] int from)//[FromForm]int userID,
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = messageApplication.SetMessageRead(int.Parse(uid), from);
                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

    }
}
